#/bin/sh
OARGS="-g"
echo "Searching for $1. Please Wait..."
if [ `sqlite3 ~/.sys/db/xshortcut.db "SELECT COUNT(location) FROM shortcut WHERE scname = '$1'"` = 1 ]; then
	if [ $2 ]; then
		open $OARGS -a `sqlite3 ~/.sys/db/shortcut.db "SELECT location FROM shortcut WHERE scname = '$1'"` $2
		exit
	fi

	open $OARGS "`sqlite3 ~/.sys/db/xshortcut.db "SELECT location FROM shortcut WHERE scname = '$1'"`"

	exit;
fi

if [ `sqlite3 ~/.sys/db/files.db "SELECT COUNT(*) FROM files WHERE location LIKE '%$1%.app'"` != 1 ]; then
	echo "Command is not unique."
	sqlite3 ~/.sys/db/files.db "SELECT * FROM files WHERE location LIKE '%$1%.app'"
	echo "------"
	echo "Want to define a shortcut (Y/N)?"
	read SC

	if [ "$SC" = "n" ] ; then
		exit;
	fi

	echo "------"
	echo "Please enter the Complete Name of the Application (like Script\ Editor):"
	read APP
	export SCAPP=`sqlite3 ~/.sys/db/files.db "SELECT * FROM files WHERE location LIKE '%$APP.app'"`
	echo $SCAPP
	echo "------"
	echo "Is this the desired application?"
	read SC

	if [ $SC = "n" ] ; then
		exit;
	fi
	echo "------"
	echo "Enter the desired Shortcut name:"
	read SCNAME
	sqlite3 ~/.sys/db/xshortcut.db "INSERT INTO shortcut VALUES('$SCNAME', '$SCAPP')"
	echo "------"
	echo "Shortcut added. Type 'x.sh $SCNAME' to run shortcut"	
	exit;
fi
if [ $2 ]; then
	open $OARGS -a `sqlite3 ~/.sys/db/files.db "SELECT * FROM files WHERE location LIKE '%$1%.app'"` $2
	exit
fi
open $OARGS "`sqlite3 ~/.sys/db/files.db "SELECT * FROM files WHERE location LIKE '%$1%.app'"`"

